package com.bdult.ozworld.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bdult.ozworld.common.Constants;
import com.bdult.ozworld.common.BreadcrumbUtil;
import com.bdult.ozworld.service.ContentsService;
import com.bdult.ozworld.common.PageUtil;

@Controller
public class ViewController {
	
	@Autowired
	private BreadcrumbUtil breadcrumbUtil;
	
	@Autowired
	private MessageSource messageSource;
	
	@Autowired
	private PageUtil pageUtil;
	
	@Autowired
	private ContentsService contentsService; 

	@RequestMapping(value="main.do")
	public ModelAndView main() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("view/main");
	
		return mv;
	}
	
	private final String BACK_URL = "backUrl";
	
	/** 카테고리(대) 페이지
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value = "category.do", method = RequestMethod.GET)
	public ModelAndView category( @RequestParam Map<String,Object> paramMap, HttpServletRequest request ) {
		
		request.getSession().setAttribute(BACK_URL, request.getContextPath()+request.getServletPath()+"?"+request.getQueryString());
		
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("view/category");
		
		Map<String, Object> cateDetail = contentsService.cateDetail(paramMap);
		mav.addObject("cateDetail", cateDetail);
		mav.addObject(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME, Constants.BREADCRUMB_BIG_CATE));
		
		//페이징 
		int totalCnt = contentsService.contentsCountByCate(paramMap);
		int pageNum = setPage(paramMap);
		mav.addObject("pageLink", pageUtil.getPageLinkMap(totalCnt, pageNum));
		mav.addObject("contentListByCate", contentsService.contentsListByCate(paramMap));
		return mav;
	}
	
	/** 시리즈 페이지
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value = "series.do", method = RequestMethod.GET)
	public ModelAndView series( @RequestParam Map<String,Object> paramMap ) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("view/series");
		mav.addObject(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME, Constants.BREADCRUMB_SERIES));
		
		mav.addObject("seriesDetail", contentsService.seriesDetail(paramMap));
		
		//페이징 
		int totalCnt = contentsService.contentsCountBySeries(paramMap);
		int pageNum = setPage(paramMap);
		mav.addObject("pageLink", pageUtil.getPageLinkMap(totalCnt, pageNum));
		mav.addObject("contentList", contentsService.contentsListBySeries(paramMap));
		return mav;
	}
	
	/** 페이지 처리
	 * @param paramsMap
	 * @param boardMap
	 * @return
	 */
	private int setPage(Map<String, Object> paramsMap) {
		int pageNum = 1; //시작글 번호
		Object obj = paramsMap.get("pageNum");
		if(obj != null){
			try {
				pageNum = Integer.valueOf(obj.toString());
			} catch (NumberFormatException nfe) {
				nfe.printStackTrace();
			}
		}
		paramsMap.put(PageUtil.ROWNUM_KEY, (pageNum - 1) * PageUtil.PER_PAGE);
		paramsMap.put(PageUtil.PER_KEY, PageUtil.PER_PAGE);
		return pageNum;
	}
	
	@RequestMapping(value = "play.do", method = RequestMethod.GET)
	public ModelAndView play( @RequestParam Map<String,Object> paramMap ) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("view/play");
		mav.addObject(Constants.BREADCRUMBS, breadcrumbUtil.getBreadcrumbs(Constants.BREADCRUMB_HOME));
		
		mav.addObject("contents", contentsService.detail(paramMap));
		return mav;
	}
	
	@RequestMapping(value = "streaming.do", method = RequestMethod.GET)
	public String StreamingTest( @RequestParam String contents_id ) {
		String redirectUrl = contentsService.movieUrlByContentsId(contents_id);
		return "redirect:"+redirectUrl;
		
	}
}
